# @Time:   2021/2/26 17:32
# @Author: Zong Wei

from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class SalesConfigPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='销售相关']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='销售配置批量修改']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")
    add_conf_button = (By.ID, "btnadd")
    edit_target = (By.ID, "datagrid-row-r3-2-0")
    edit_conf_button = (By.XPATH, "//span[.='批量更改默认销售小组']")

    group = (By.XPATH, "//select[@id='DefaultSalesTeamID']/following::a")
    group_value = (By.XPATH, "//*[@id='datagrid-row-r1-2-0']/td[@field='SalesTeamCode']/div")

    partner_name = (By.ID, "PartnerNameCN")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='简称中文']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def sales_config_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def search_config(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.partner_name)).send_keys('上海静初化工物流有限公司')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def add_edit_config(self):
        self.search_config()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.add_conf_button)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_target)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_conf_button)).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.element_to_be_clickable(self.group)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.group_value)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("销售配置修改完成")


if __name__ == '__main__':
    pg = SalesConfigPage()
    pg.sales_config_home()
    pg.add_edit_config()

